package com.lemon.secondhandplatform.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lemon.secondhandplatform.BaseApplication;
import com.lemon.secondhandplatform.R;
import com.lemon.secondhandplatform.mode.Good;
import com.lemon.secondhandplatform.mode.User;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import de.hdodenhof.circleimageview.CircleImageView;

public class GoodAdapter extends RecyclerView.Adapter<GoodAdapter.GoodHolder> {

    private OnGoodItemClickListener onGoodItemClickListener;

    private Context context;
    private List<Good> data;

    public GoodAdapter(Context context, List<Good> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public GoodHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_good, parent, false);
        return new GoodHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull GoodHolder holder, int position) {
        Good good = data.get(position);
        holder.bind(good);
    }


    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    /**
     * 刷新数据
     *
     * @param data
     */
    public void refresh(List<Good> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    /**
     * 追加数据
     *
     * @param data
     */
    public void append(List<Good> data) {
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    public void setOnGoodItemClickListener(OnGoodItemClickListener onGoodItemClickListener) {
        this.onGoodItemClickListener = onGoodItemClickListener;
    }

    class GoodHolder extends RecyclerView.ViewHolder {

        private Good good;

        //卖家头像
        @BindView(R.id.imgAvator)
        CircleImageView imgAvator;
        //卖家昵称
        @BindView(R.id.tvSellerNickname)
        TextView tvSellerNickname;
        //商品略缩图
        @BindView(R.id.imgGoodPhoto)
        ImageView imgGoodPhoto;
        //商品介绍
        @BindView(R.id.tvGoodIntroduction)
        TextView tvGoodIntroduction;
        //喜欢
        @BindView(R.id.imgBtnLike)
        ImageView imgBtnLike;
        //喜欢该商品的人数
        @BindView(R.id.tvLikesCounter)
        TextView tvLikesCounter;

        public GoodHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bind(final Good good) {
            this.good = good;
            Glide.with(context)
                    .load(BaseApplication.getUser().getAvator())
                    .into(imgAvator);
            Glide.with(context)
                    .load(good.getThumbnailURL())
                    .into(imgGoodPhoto);

            tvSellerNickname.setText(good.getSeller().getNickname());
            tvGoodIntroduction.setText(good.getIntroduction());
            tvLikesCounter.setText(good.getLikeCounter().toString());

            BmobQuery<User> query = new BmobQuery<>();
            query.addWhereRelatedTo("likes", new BmobPointer(good));
            query.findObjects(new FindListener<User>() {
                @Override
                public void done(List<User> list, BmobException e) {
                    if (e == null) {
                        if (list.contains(BaseApplication.getUser())) {
                            good.setLike(true);
                            imgBtnLike.setImageResource(R.drawable.ic_heart_red);
                        } else {
                            good.setLike(false);
                            imgBtnLike.setImageResource(R.drawable.ic_heart_gray);
                        }
                    }
                }
            });
        }

        /**
         * 喜欢
         * @param v
         */
        @OnClick(R.id.imgBtnLike)
        public void like(View v) {
            if (onGoodItemClickListener != null) {
                onGoodItemClickListener.onLikeClick(itemView, good);
            }
        }

        /**
         * 评论
         * @param v
         */
        @OnClick(R.id.imgBtnComments)
        public void comments(View v) {
            if (onGoodItemClickListener != null) {
                onGoodItemClickListener.onCommentsClick(v, good);
            }
        }

        /**
         * 更多
         * @param v
         */
        @OnClick(R.id.imgBtnMore)
        public void more(View v) {
            if (onGoodItemClickListener != null) {
                onGoodItemClickListener.onMoreClick(v, good);
            }
        }

        /**
         * 预定
         * @param v
         */
        @OnClick(R.id.tvReserve)
        public void reserve(View v) {
            if (onGoodItemClickListener != null) {
                onGoodItemClickListener.onReserveClick(v, good);
            }
        }
    }

    public interface OnGoodItemClickListener {

        void onLikeClick(View v, Good good);

        void onCommentsClick(View v, Good good);

        void onMoreClick(View v, Good good);

        void onReserveClick(View v, Good good);
    }
}
